package org.softmed.rest.admin.mediaType;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;

import org.softmed.rest.admin.DetailLevelListener;
import org.softmed.rest.admin.DetailChooser.DetailLevel;
import org.softmed.rest.admin.handler.HandlerManagerHeader;
import org.softmed.rest.admin.interfaces.EventListener;
import org.softmed.rest.admin.interfaces.FileHandler;
import org.softmed.rest.admin.interfaces.Recompiler;
import org.softmed.rest.admin.interfaces.SelectionPathChanged;
import org.softmed.rest.admin.interfaces.Validator;
import org.softmed.rest.admin.module.ValidationException;
import org.softmed.rest.admin.interfaces.RemoveListener;

public class MediaTypeHandlerManager extends JPanel implements RemoveListener,
		Validator, Recompiler, SelectionPathChanged, DetailLevelListener {

	Map<String, String> handlers;
	HandlerManagerHeader handlerManagerHeader = new HandlerManagerHeader();
	List<MediaTypeEditor> choosers = new ArrayList<MediaTypeEditor>();
	FileHandler fileHandler;

	public MediaTypeHandlerManager() {
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
//		setBackground(Color.BLACK);
		setMinimumSize(new Dimension(100, 25));
		add(handlerManagerHeader);
	}

	public Map<String, String> getHandlers() {
		return handlers;
	}

	public void setHandlers(Map<String, String> getHandlers) {
		this.handlers = getHandlers;
		refresh();
	}

	public void refresh() {
		removeAll();
		add(handlerManagerHeader);
		Set<String> keys = handlers.keySet();
		for (String mediaType : keys) {
			MediaTypeEditor chooser = new MediaTypeEditor();
			chooser.getMediaTypeChooser().setMediaTypeDescription(mediaType);
			chooser.getHandlerChooser().setText(handlers.get(mediaType));
			addChooser(chooser);
		}
		if (choosers.size() > 0)
			handlerManagerHeader.setVisible(true);
		else
			handlerManagerHeader.setVisible(false);
	}

	public void addChooser(MediaTypeEditor chooser) {
		chooser.setRemoveListener(this);
		chooser.setValidator(this);
		chooser.setRecompiler(this);
		chooser.setFileHandler(fileHandler);
		add(chooser);
		choosers.add(chooser);
		handlerManagerHeader.setVisible(true);
		revalidate();
	}

	@Override
	public void remove(Object source) {

		MediaTypeEditor chooser = (MediaTypeEditor) source;
		if (chooser.getMediaTypeChooser().getMediaTypeDescription() != null)
			handlers.remove(chooser.getMediaTypeChooser()
					.getMediaTypeDescription());
		remove(chooser);
		choosers.remove(chooser);
		if (choosers.size() > 0)
			handlerManagerHeader.setVisible(true);
		else
			handlerManagerHeader.setVisible(false);

		revalidate();

	}

	@Override
	public boolean validate(Object object) {
		MediaTypeChooser received = (MediaTypeChooser) object;

		String validateMediaType = received.getMediaTypeDescription();
		String mediaType = null;

		for (MediaTypeEditor chooser : choosers) {
			mediaType = chooser.getMediaTypeChooser().getMediaTypeDescription();
			if (chooser.getMediaTypeChooser() == received)
				continue;

			if (mediaType.equals(validateMediaType))
				return false;
		}

		return true;
	}

	@Override
	public void recompile() {
		handlers.clear();
		String path = null, mediaType = null;

		for (MediaTypeEditor chooser : choosers) {
			mediaType = chooser.getMediaTypeChooser().getMediaTypeDescription();
			path = chooser.getHandlerChooser().getText();
			handlers.put(mediaType, path);
		}
	}

	public void validateData() throws ValidationException {
		// recompile();
		for (MediaTypeEditor mediaTypeEditor : choosers) {
			// validate(mediaTypeEditor);
			mediaTypeEditor.validateData();
		}

	}

	public void addNewMediaTypeHandler() {
		MediaTypeEditor chooser = new MediaTypeEditor();
		addChooser(chooser);

	}

	public List<String> getMediaTypes() {
		return new ArrayList<String>(handlers.keySet());
	}

	public void selectionPathChanged(boolean directory, String name,
			String parent) {
		for (MediaTypeEditor mediaTypeEditor : choosers) {
			mediaTypeEditor.selectionPathChanged(directory, name, parent);
		}

	}

	public FileHandler getFileHandler() {
		return fileHandler;
	}

	public void setFileHandler(FileHandler fileHandler) {
		this.fileHandler = fileHandler;
		for (MediaTypeEditor mediaTypeEditor : choosers) {
			mediaTypeEditor.setFileHandler(fileHandler);
		}
	}

	@Override
	public void detailLevelChanged(DetailLevel detailLevel) {
		switch (detailLevel) {
		case MEDIA_TYPE:
//			this.setVisible(true);
			break;
		}

	}

}
